﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MultLanguage.NLS;
using System.Windows.Forms;
using MultLanguage.ConfigFile;
using LogLib;

namespace MultLanguage
{
    public class MultLanguageService
    {
        public static bool InitSuccessed
        {
            get
            {
                if (instance != null)
                {
                    return instance._initSuccess;
                }
                return false;
            }
        }

        private static MultLanguageService instance;

        private static object _lock = new object();

        public static MultLanguageService GetInstance()
        {
            if (instance == null)
            {
                lock (_lock)
                {
                    if (instance == null)
                    {
                        instance = new MultLanguageService();
                    }
                }
            }
            return instance;
        }

        private string _strError = "";
        public string ErrorMessage
        {
            get
            {
                return _strError;
            }
        }

        private List<string> _listErrors = new List<string>();
        public List<string> Errors
        {
            get
            {
                return _listErrors;
            }
        }

        private List<MLConfigFile> listConfigFiles = new List<MLConfigFile>();
        
        private Dictionary<string, Dictionary<string, string>> _data = new Dictionary<string, Dictionary<string, string>>();

        public Dictionary<string, string> LocalLanguages = new Dictionary<string, string>();

        private SLConfigFile configFile = null;

        private bool _initSuccess = false;
      
        public MultLanguageService()
        {

        }

        public bool InitService()
        {
            if (_initSuccess)
            {
                return true;
            }
            _strError = "";
            string filePath = string.Format("{0}//MultLanguage//NationalLanguageCodes.xml", Application.StartupPath);
            configFile = new SLConfigFile(filePath);
            if (!configFile.Init())
            {
                _strError = "多语言配置文件加载错误! \n错误详情: " + configFile.ErrorMessage;
                return false;
            }
            LocalLanguages = configFile.LocalLanguages;
            List<string> codes = LocalLanguages.Keys.ToList();
            foreach (string code in codes)
            {
                _strError = "";
                string strFilePath = string.Format("{0}//MultLanguage//Language_{1}.xml", Application.StartupPath, code);
                MLConfigFile mls = new MLConfigFile(strFilePath);
                if (!mls.Init())
                {
                    _strError = string.Format("代码为{0}语言配置文件加载错误! \n错误详情: {1}",code, configFile.ErrorMessage);
                    _listErrors.Add(_strError);
                    continue;   
                }
                Dictionary<string, string> collection = mls.Collection;
                _data.Add(code, collection);
                listConfigFiles.Add(mls);
            }
            _initSuccess = true;
            return true;
        }

        public string this[string code, string source]
        {
            get 
            {
                if (code.Equals("zh-CN"))
                {
                    return source;
                }
                if (!_initSuccess || LocalLanguages == null || _data == null)
                {
                    return source;
                }
                if (!LocalLanguages.ContainsKey(code))
                {
                    string strLog = string.Format("本地不存在区域语言代码[{0}]的配置文件", code);
                    Log.WriteException(strLog);
                    return source;
                }
                if (!_data.ContainsKey(code))
                {
                    string strLog = string.Format("MLConfigFile服务类未能成功加载区域语言代码[{0}]的配置文件", code);
                    Log.WriteException(strLog);
                    return source;
                }
                Dictionary<string, string> dic = _data[code];
                if (dic != null)
                {
                    if (!dic.ContainsKey(source))
                    {
                        string strLog = string.Format("关键字[{0}]不在字典中.", source);
                        Log.WriteException(strLog);
                        return source;
                    }
                    else
                    {                   
                        return dic[source];
                    }                   
                }
                else
                {
                 //   string strLog = string.Format("不存在区域语言代码[{0}]的配置文件", code);
                    //Log.WriteException(strLog);
                    return source;
                }
            }
        }
  
    }
}
